package com.comum.javahomework420242025.controller;

import com.comum.javahomework420242025.pojo.AskList;
import com.comum.javahomework420242025.pojo.PageBean;
import com.comum.javahomework420242025.pojo.Result;
import com.comum.javahomework420242025.pojo.WeeklyReport;
import com.comum.javahomework420242025.repository.AllFileRepository;
import com.comum.javahomework420242025.repository.WeeklyReportRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

@RestController
public class WeeklyReportController {
    @Autowired
    private AllFileRepository allFileRepository;
    @Autowired
    private WeeklyReportRepository weeklyReportRepository;
    @PostMapping("/addWeeklyReport")
    public Result addWeeklyReport(@RequestBody WeeklyReport weeklyReport){
        weeklyReport.setId(null);

        weeklyReportRepository.save(weeklyReport);
        return Result.success();
    }
    @PutMapping("/updateWeeklyReport")
    public Result updateWeeklyReport(@RequestBody WeeklyReport weeklyReport){
        weeklyReportRepository.save(weeklyReport);
        return Result.success();
    }
    @DeleteMapping("/deleteWeeklyReport")
    public Result deleteWeeklyReport(String id){
        System.out.println(id);
        weeklyReportRepository.deleteById(Long.valueOf(id));
        String type = "公司信息";
        allFileRepository.deleteByElseIdAndType(id,type);
        return Result.success();
    }
    @PostMapping("/getWeeklyReport")
    public Result<PageBean<WeeklyReport>> getWeeklyReport(@RequestBody AskList askList){
        int pageSize = askList.getPageSize();
        int pageNum = askList.getPageNum();
        String str = askList.getStr();
        String type = askList.getFindType();
        PageBean<WeeklyReport> pb = new PageBean<>();
        Pageable pageable = PageRequest.of(pageNum-1,pageSize);
        Page<WeeklyReport> page = weeklyReportRepository.list(str,pageable);
        pb.setTotal(page.getTotalElements());
        pb.setItems(page.getContent());
        System.out.println(pb);
        return Result.success(pb);


    }

}
